package com.newsproject.mapper;


import com.newsproject.pojo.*;
import com.newsproject.pojo.DTO.PostDTO;
import com.newsproject.pojo.DTO.UpdatePost;
import org.apache.ibatis.annotations.*;
import com.newsproject.pojo.Comment;
import com.newsproject.pojo.Post;
import com.newsproject.pojo.vo.CommentVo;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import java.util.List;
import java.util.Map;

@Mapper
public interface NewsMapper {
    /**
     * 添加新闻
     * @param post
     */
    @Insert("insert into post (user_id,content,tag_id,cover,category_id,is_member_only,title,text_status) " +
            "values (#{userId},#{content},#{tagId},#{coverImage},#{categoryId},#{isMemberOnly},#{title},#{textStatus})")
    @Options(useGeneratedKeys = true, keyProperty = "postId")
    boolean save(Post post);
    @Insert("insert into audit_post(post_id,admin_id,admin_name,category_id,target_type,audit_time,audit_result,audit_reason)" +
            "values (#{postId},#{adminId},#{adminName},#{categoryId},#{targetType},#{auditTime},#{auditResult},#{auditReason})")
    int auditPost(AuditPost auditPost);
    /**
     * 新闻转发
     * @param repost
     * @return
     */
    @Insert("insert into repost(user_id,post_id,created_time,forwarded_id)values (#{userId},#{postId},#{createdTime},#{forwardedId})")
    boolean insertRepost(Repost repost);
    @Update("UPDATE post SET share_count = share_count + 1 WHERE post_id = #{postId}")
    int incrementShareCount(int postId);
    @Select("select * from user where user_id=#{userId}")
    User selectbyId(int userId);
    /**
     * 关注
     * @param follow
     * @return
     */
    @Insert("insert into follow (follower_id,followed_id)values (#{followerId},#{followedId})")
    boolean follower(Follow follow);

    /**
     * 取消关注
     * @param followerId
     * @param followedId
     */
    @Delete("delete from follow where follower_id=#{followerId} and followed_id=#{followedId}")
    void delectfollower(@Param("followerId")int followerId,@Param("followedId")int followedId);

    /**
     * 查询用户是否已经关注过该博主
     * @param followerId
     * @param followedId
     * @return
     */
    @Select("select count(*) from follow where follower_id=#{followerId} and followed_id=#{followedId}")
    int checkfollow(@Param("followerId")int followerId,@Param("followedId")int followedId);

    /**
     * 添加新闻评论回复
     * @param comment
     * @return
     */
    @Insert("insert into comment (user_id,post_id,content,parent_id)" +
            "values (#{userId},#{postId},#{content},#{parentId})")
    boolean insertComment(Comment comment);
    @Update("UPDATE post SET comment_count = comment_count+1 WHERE post_id = #{postId}")
    int addCommentCount(int postId);
    @Update("UPDATE post SET comment_count = comment_count-1 WHERE post_id = #{postId}")
    int reduceCommentCount(int postId);
    @Select("select *from comment where comment_id=#{commentId}")
    Comment selectcomment(int commentId);
    @Select("select count(*)from comment where parent_id=#{commentId}")
            int selectparentcount(int commentId);
    // 查询文章下的所有评论及回复
    List<Comment> selectCommentsByPostId(Integer postId);

    // 根据父评论ID查询回复
    List<Comment> selectRepliesByParentId(Integer parentId);
    /**
     * 删除新闻评论
     * @param commentId
     * @return
     */
    @Delete("delete from comment where comment_id=#{commentId}")
    boolean deleteComment(int commentId);
    void commentCount(int postId);
    /**
     * 查询当前用户的评论记录
     * @param postId
     * @return
     */
    @Select("select  count(*) from comment where post_id=#{postId} ")
    int countcomment(@Param("postId")int postId);

    /**
     * 查询当前新闻的所有评论
     * @param postId
     * @return
     */
    @Delete("delete  from comment where post_id =#{post_id} ")
    int deleteCommentByPostId(@Param("post_id")int postId);

    /**
     * 添加评论点赞
     * @param userId
     * @param commentId
     */
    @Insert("insert into comment_like (user_id,comment_id) values (#{userId},#{commentId})")
    void insertLike(@Param("userId")int userId,@Param("commentId")int commentId);
    @Update("update comment set likecount=#{likeCount} where comment_id=#{commentId}")
    void updateCommentlikeCount(@Param("likeCount")int likeCount,@Param("commentId")int commentId);
    /**
     * 根据用户 ID 和评论 ID 删除评论点赞记录
     * @param params
     * @return
     */
    @Delete("delete from comment_like where user_id=#{userId} and  comment_id=#{commentId}")
    void deleteLike(Map<String,Object>params);
    /**
     * 检查用户是否已经点赞过该评论
     * @param params 包含 userId 和 commmentId 的参数
     * @return 评论点赞记录数
     */
    @Select("select count(*) from comment_like where user_id=#{userId} and comment_id=#{commentId}")
    int checkLikeRecord(Map<String, Object> params);

    /**
     * 查询当前评论的所有点赞数
     * @param commentId
     * @return
     */
    @Select("select count(*) from comment_like where comment_id=#{commentId}")
    int  countCommentLike(@Param("commentId")int commentId);
    /**
     * 添加新闻点赞
     * @param userId
     * @param postId
     * @return
     */
    @Insert("insert into heart (user_id,post_id) values (#{userId},#{postId})")
    void insertHeart(@Param("userId") int userId, @Param("postId") int postId);
    @Update("UPDATE post SET like_count = like_count+1 WHERE post_id = #{postId}")
    int addpostLike(int postId);
    /**
     * 根据用户 ID 和新闻类型 ID 删除点赞记录
     * @param params
     * @return
     */
    @Delete("delete from heart where user_id=#{userId} and  post_id=#{postId}")
    void deleteHeart(Map<String,Object>params);
    @Update("UPDATE post SET like_count = like_count-1 WHERE post_id = #{postId}")
    int deletepostLike(int postId);
    /**
     * 检查用户是否已经点赞过该新闻类型
     * @param   userId 和 newsTypeId 的参数
     * @return 点赞记录数
     */
    @Select("select like_id from heart where user_id=#{userId} and post_id=#{postId}")
    Integer checkHearyRecord(@Param("userId") Integer userId,@Param("postId") Integer postId);
    @Delete("delete  from heart where like_id =#{likeId} ")
    void deleteHeartById(@Param("likeId")Integer likeId);
    /**
     * 添加新闻收藏数量
     * @param userId
     * @param postId
     */
    @Insert("insert into bookmark (user_id,post_id) values (#{userId},#{postId})")
    void insertbookmark(@Param("userId")int userId,@Param("postId")int postId);
    @Update("UPDATE post SET bookmark_count = bookmark_count+1 WHERE post_id = #{postId}")
    int addbookmarkCount(int postId);
    /**
     * 根据用户 ID 和评论 ID 删除新闻点赞记录
     * @param
     * @return
     */
    void deletebookmark(@Param("user_id")int userId,@Param("post_id")int postId);
    @Update("UPDATE post SET bookmark_count = bookmark_count-1 WHERE post_id = #{postId}")
    int declinebookmarkCount(int postId);
    @Select("select count(*) from bookmark where user_id=#{userId} and post_id=#{postId}")
    int checkbookmark(@Param("userId") Integer userId,@Param("postId") Integer postId);
    //查询粉丝列表
    @Select("SELECT follower_id FROM follow WHERE followed_id = #{followedId}")
    List<Integer> selectFollowerIds(Integer followedId);

    List<User> selectFollowerList(@Param("followerIds") List<Integer> followerIds);
    //查询关注列表
    @Select("SELECT followed_id FROM follow WHERE follower_id = #{followerId}")
    List<Integer> selectFollowedIds(Integer followedId);

    // 使用 @Param 和 foreach 查询多个用户信息
    @Select("<script>" +
            "SELECT user_id, username,avatar FROM user " +
            "WHERE user_id IN " +
            "<choose>" +
            "  <when test='followedIds != null and followedIds.size() > 0'>" +
            "    <foreach item='item' collection='followedIds' open='(' separator=',' close=')'>" +
            "      #{item}" +
            "    </foreach>" +
            "  </when>" +
            "  <otherwise>" +
            "    (NULL)" +
            "  </otherwise>" +
            "</choose>" +
            "</script>")
    List<User> selectFollowedList(@Param("followedIds") List<Integer> followedIds);
    //统计关注的总数
    @Select("select count(*)from follow where follower_id=#{followerId}")
    int countFollower(Integer followerId);
    //统计粉丝的总数
    @Select("select count(*)from follow where followed_id=#{followedId}")
    int countFollowed(Integer followedId);
    @Select("SELECT COALESCE(SUM(like_count), 0) FROM post WHERE user_id=#{userId}")
    int countAlllike(Integer userId);
    List<Post> selectHeartList(Integer userId);
    List<Post> bookmarkList(Integer userId);

    //删除post
    int  deletePostByPostId(@Param("post_id")Integer postId);



    //计算并比较热度
    List<Post> calculateAndCompareHot();
    //void insertHotPost(@Param("hotPosts")List<Post> hotPosts);
    List<Integer> selectHotPostId();
    //查询今天的热搜
    List<HotSearch> selectTodayHotSearch(@Param("startIndex") int start, @Param("page_size") int count);
    //发布新闻
    void insertPost(@Param("post")Post post);


    List<HotSearch> selectHotSearch();
     //根据post_id查询post
     Post selectPostByPostId(@Param("post_id")int postId);

    //查询评论
     List<CommentVo> selectFourCommentByPostId(@Param("post_id")int postId);
     List<CommentVo> selectAllCommentByPostId(@Param("post_id")int postId);
     //删除评论
     void deleteByCommentId(@Param("comment_id")int comment_id);



    //根据标签查询post
    List<PostDTO> selectPostByPageAndTagId(@Param("start_size") int startSize,@Param("page_size") int pageSize,@Param("tag_id")int tag_id);

    //查看关注的用户的新闻
    List<PostDTO> selectPostByFollowedId(@Param("start_size") int startSize,@Param("page_size") int pageSize,@Param("user_id")int userId);
    //查询关注用户的新闻的总条数
    int totalCountByFollowedId(@Param("user_id")int userId);
    //添加热搜
    void insertHotPost(@Param("hotPosts") List<Post> hotPosts);
    //删除热搜
    void deleteHotPost(@Param("post_id")Integer postId);
    //根据热度进行搜索
    List<Post> selectHotPostByHeat(@Param("start_size") int startSize,@Param("page_size") int pageSize);

    //查询收藏的文章
    List<PostDTO> selectPostByBookmarkId(@Param("user_id")int userId,@Param("start_index") Integer startIndex);
    //查询收藏的文章的总条数
    Integer totalCountByBookmarkId(@Param("user_id")int userId);


     @Insert("insert into comment (content,user_id,post_id) values (#{content},#{userId},#{postId})")
     void comment(@Param("content") String content,@Param("userId")int user_id,@Param("postId")Integer postId);


    /*
    * 根据标签查询新闻咨询的总数
    * 新闻咨询是已审核的
    * */
    @Select("select count(*) from post where tag_id = #{tagId} and text_status = 0 and audit_status =1  ")
    Integer selectTotalCountByTagId(@Param("tagId") int tag_id);
    //根据用户id和文章类型查询文章
    List<PostDTO> selectPostByPostTypeAndUserId(@Param("user_id")int userId, @Param("category_id")int category_id,@Param("start_index") Integer startIndex);
    //根据用户id和类别查询文章的总数
    Integer selectTotalCountByUserIdAndCategoryId(@Param("user_id")int userId,@Param("category_id")int category_id, @Param("text_status")Integer textStatus,@Param("audit_status")Integer auditStatus);


    //manage
    void updatePost(@Param("post") Post post);
    /**
     * 查询草稿箱
     * @param userId 用户id
     * @param startIndex 起始索引
     * @return List<PostDTO> 草稿箱列表
     */
    List<PostDTO> selectDraftPostByPage(@Param("start_index") Integer startIndex,@Param("user_id")int userId);
    /**
     * 查询草稿箱的总条数
     * @param userId 用户id
     * @return int 草稿箱总条数
     */
    Integer selectDraftPostCount(@Param("user_id")int userId);


    /**
     * 更新文章的阅读量
     * @param postId
     * @return int 影响行数
     */
    @Update("update post set view_count = view_count+1 where post_id = #{post_id}")
    int updateViewCount(@Param("post_id")int postId);


    /**
     * 根据文章id查询文章
     * @param postId
     * @return PostDTO
     */
    UpdatePost findUpdatePostByPostId(@Param("post_id")int postId);

}
